<?php 
#############################################################################
#  Module Mini Webboard for Opencart 1.4.8 Up  From Team SiamOpencart.com		  											   #
#  เว็บผู้พัฒนา www.siamopencart.com ,www.thaiopencart.com                                                                                 #
#  โดย Somsak2004 วันที่ 23 พฤศจิกายน 2553                                                                                                           #
#############################################################################
# โดยการสนับสนุนจาก                                                                                                                                            #
# Unitedsme.com : ผู้ให้บริการเช่าพื้นที่เว็บไซต์ จดโดเมน ระบบ Linux                                                                              #
# Net-LifeStyle.com : ผู้ให้บริการเช่าพื้นที่เว็บไซต์์ จดโดเมน ระบบ Linux																           #
# SiamWebThai.com : SEO ขั้นเทพ โปรโมทเว็บขั้นเซียน ออกแบบ พัฒนาเว็บไซต์ / ตามความต้องการ และถูกใจ Google 		   #
#############################################################################
class ControllerWebboardNewpost extends Controller {
	private $error = array(); 
 	private $id_author;
   	private $user_name;
	public function index() {
		$this->language->load('webboard/new_post');
    	$this->document->title = $this->language->get('heading_title');  
		$this->load->model('webboard/webboard');
    	if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
           //เก็บข้อมูล	 
		  if (isset($_SESSION['user_id']) || $this->config->get('webboard_guest') =="1") {
			   //post โดยทีมงานร้าน
		  if (isset($_SESSION['user_id'])) {$this->id_author=$_SESSION['user_id'];}else{$this->id_author=0;}
	      $this->model_webboard_webboard->InsertNewPost(html_entity_decode($this->request->post['title'], ENT_QUOTES, 'UTF-8'),strip_tags($this->request->post['enquiry']),$this->id_author);
    	} else {
			 //post ลุกค้า
  		   $this->load->model('account/customer');
		   $customer_info = $this->model_account_customer->getCustomer($this->customer->getId());
		   $this->user_name=$customer_info['firstname'];
 	       $this->model_webboard_webboard->InsertNewPostByUser(html_entity_decode($this->request->post['title'], ENT_QUOTES, 'UTF-8'),strip_tags($this->request->post['enquiry']),$this->user_name );
		}
	      $this->redirect((HTTPS_SERVER . 'index.php?route=webboard/new_post/success'));
		}
      	$this->document->breadcrumbs = array();

      	$this->document->breadcrumbs[] = array(
        	'href'      => (HTTP_SERVER . 'index.php?route=common/home'),
        	'text'      => $this->language->get('text_home'),
        	'separator' => FALSE
      	);

      	$this->document->breadcrumbs[] = array(
        	'href'      => (HTTP_SERVER . 'index.php?route=webboard/new_post'),
        	'text'      => $this->language->get('heading_title'),
        	'separator' => $this->language->get('text_separator')
      	);	
			
    	$this->data['heading_title'] = $this->language->get('heading_title');
    	$this->data['text_new'] = $this->language->get('text_new');
    	$this->data['text_detail'] = $this->language->get('text_detail');
    	$this->data['text_send'] = $this->language->get('text_send');
		$this->data['entry_enquiry'] = $this->language->get('entry_enquiry');
		$this->data['entry_captcha'] = $this->language->get('entry_captcha');

		if (isset($this->error['title'])) {
    		$this->data['error_title'] = $this->error['title'];
		} else {
			$this->data['error_title'] = '';
		}

		if (isset($this->error['enquiry'])) {
			$this->data['error_enquiry'] = $this->error['enquiry'];
		} else {
			$this->data['error_enquiry'] = '';
		}		
		
 		if (isset($this->error['captcha'])) {
			$this->data['error_captcha'] = $this->error['captcha'];
		} else {
			$this->data['error_captcha'] = '';
		}	

    	$this->data['button_continue'] = $this->language->get('button_continue');
    
		$this->data['action'] = (HTTP_SERVER . 'index.php?route=webboard/new_post');
    	
		if (isset($this->request->post['title'])) {
			$this->data['title'] = $this->request->post['title'];
		} else {
			$this->data['title'] = '';
		}

	
		if (isset($this->request->post['enquiry'])) {
			$this->data['enquiry'] = $this->request->post['enquiry'];
		} else {
			$this->data['enquiry'] = '';
		}
		
		if (isset($this->request->post['captcha'])) {
			$this->data['captcha'] = $this->request->post['captcha'];
		} else {
			$this->data['captcha'] = '';
		}		
	
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/webboard/new_post.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/webboard/new_post.tpl';
		} else {
			$this->template = 'default/template/webboard/new_post.tpl';
		}
		
		$this->children = array(
			'common/header',
			'common/footer',
			'common/column_left',
			'common/column_right'
		);
		
 		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));		
  	}

  	public function success() {
		$this->language->load('webboard/new_post');

		$this->document->title = $this->language->get('heading_title'); 

      	$this->document->breadcrumbs = array();

      	$this->document->breadcrumbs[] = array(
        	'href'      => (HTTP_SERVER . 'index.php?route=webboard/new_post'),
        	'text'      => $this->language->get('text_home'),
        	'separator' => FALSE
      	);

      	$this->document->breadcrumbs[] = array(
        	'href'      => (HTTP_SERVER . 'index.php?route=webboard/new_post'),
        	'text'      => $this->language->get('heading_title'),
        	'separator' => $this->language->get('text_separator')
      	);	
		
    	$this->data['heading_title'] = $this->language->get('heading_title');

    	$this->data['text_message'] = $this->language->get('text_message');

    	$this->data['button_continue'] = $this->language->get('button_continue');

    	$this->data['continue'] = (HTTP_SERVER . 'index.php?route=webboard/board');

		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/success.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/common/success.tpl';
		} else {
			$this->template = 'default/template/common/success.tpl';
		}
		
		$this->children = array(
			'common/header',
			'common/footer',
			'common/column_left',
			'common/column_right'
		);
		
 		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression')); 
	}

	public function captcha() {
		$this->load->library('captcha');
		
		$captcha = new Captcha();
		
		$this->session->data['captcha'] = $captcha->getCode();
		
		$captcha->showImage();
	}
	
  	private function validate() {
    	if (strlen(utf8_decode($this->request->post['title'])) < 3) {
      		$this->error['title'] = $this->language->get('error_title');
    	}

    	if ((strlen(utf8_decode($this->request->post['enquiry'])) < 10) || (strlen(utf8_decode($this->request->post['enquiry'])) > 1000)) {
      		$this->error['enquiry'] = $this->language->get('error_enquiry');
    	}

    	if (!isset($this->session->data['captcha']) || ($this->session->data['captcha'] != $this->request->post['captcha'])) {
      		$this->error['captcha'] = $this->language->get('error_captcha');
    	}
		
		if (!$this->error) {
	  		return TRUE;
		} else {
	  		return FALSE;
		}  	  
  	}
}

?>
